package com.develope.menu.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

public class Alerts {
	private static final String OK = "OK"; // android.R.string.cancel;
	private static final String CANCEL = "Cancel"; // android.R.string.ok;

	// add text cancel button
	private static ProgressDialog mProgressDialog;

	public final static ProgressDialog showLoadingDialog(Activity activity,
			String message, OnClickListener cancelListener) {
		if (mProgressDialog != null) {
			mProgressDialog.dismiss();
			mProgressDialog = null;
		}
		mProgressDialog = new ProgressDialog(activity);
		if (TextUtils.isEmpty(message)) {
			message = "Loading...";
		}
		mProgressDialog.setMessage(message);
		mProgressDialog.setCancelable(false);
		if (cancelListener != null) {
			mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, CANCEL,
					cancelListener);
		}
		mProgressDialog.show();
		return mProgressDialog;
	}

	public final static void hideLoadingDialog() {
		if (mProgressDialog != null) {
			mProgressDialog.dismiss();
			mProgressDialog = null;
		}
	}

	// add to support DialogFragment - END
	public final static AlertDialog showNotifyDialog(Activity activity,
			String message) {
		return showButtonDialog(activity, 0, null, message, OK,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}, null);
	}

	public final static AlertDialog showNotifyDialog(Activity activity,
			String message, OnClickListener onDone) {
		// showButtonDialog(activity, 0, null, message, OK, onDone, null);
		return showButtonDialog(activity, null, message, OK, onDone, false);
	}

	public final static AlertDialog showErrorDialog(Activity activity,
			String title, String message) {
		return showButtonDialog(activity, android.R.drawable.ic_dialog_alert,
				title, message, OK, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}, null);
	}

	public final static AlertDialog showButtonDialog(Activity activity,
			String message, OnClickListener onOK) {
		return showButtonDialog(activity, 0, null, message, OK, onOK, null);
	}

	public final static AlertDialog showButtonDialog(Activity activity,
			String message, OnClickListener onOK, OnDismissListener onDismiss) {
		return showButtonDialog(activity, 0, null, message, OK, onOK, onDismiss);
	}

	public final static AlertDialog showButtonDialog(Activity activity,
			String title, String message, String labelOK, OnClickListener onOK) {
		return showButtonDialog(activity, 0, title, message, labelOK, onOK,
				null);
	}

	public final static AlertDialog showButtonDialog(Activity activity,
			String message, String labelOK, OnClickListener onOK) {
		return showButtonDialog(activity, 0, null, message, labelOK, onOK, null);
	}

	public final static AlertDialog showButtonDialog(Activity activity,
			int icon, String title, String message, String labelOK,
			OnClickListener onOK, OnDismissListener onDismiss) {
		if (activity.isFinishing())
			return null;
		if (TextUtils.isEmpty(labelOK)) {
			labelOK = OK;
		}
		AlertDialog dialog = new AlertDialog.Builder(activity)
				.setMessage(message).setPositiveButton(labelOK, onOK).create();
		if (icon != 0) {
			dialog.setIcon(icon);
		}
		if (!TextUtils.isEmpty(title)) {
			dialog.setCustomTitle(createTitleView(activity, title));
		}
		if (onDismiss != null) {
			dialog.setOnDismissListener(onDismiss);
		}
		dialog.show();
		return dialog;
	}

	public final static void showButtonDialog(Activity activity,
			String message, String labelOK, OnClickListener onOK,
			boolean cancelable) {
		if (activity.isFinishing())
			return;
		AlertDialog dialog = new AlertDialog.Builder(activity)
				.setMessage(message).setPositiveButton(labelOK, onOK).create();
		dialog.setCancelable(cancelable);
		dialog.show();
	}

	public final static AlertDialog showDialogWithoutButton(Activity activity,
			String message, boolean cancelable) {
		if (activity.isFinishing())
			return null;
		AlertDialog dialog = new AlertDialog.Builder(activity).setMessage(
				message).create();
		dialog.setCancelable(cancelable);
		dialog.show();

		return dialog;
	}

	public final static AlertDialog showButtonDialog(Activity activity,
			String title, String message, String labelOK, OnClickListener onOK,
			boolean cancelable) {
		if (activity.isFinishing())
			return null;
		AlertDialog dialog = new AlertDialog.Builder(activity)
		// .setTitle(title)
				.setMessage(message).setPositiveButton(labelOK, onOK).create();
		if (!TextUtils.isEmpty(title)) {
			dialog.setCustomTitle(createTitleView(activity, title));
		}
		dialog.setCancelable(cancelable);
		dialog.show();
		return dialog;
	}

	/************************ support 2 buttons *****************************************************/
	public final static void show2ButtonDialog(Activity activity, String title,
			String message, OnClickListener onOK) {
		show2ButtonDialog(activity, title, message, OK, CANCEL, onOK, null,
				null);
	}

	public final static void show2ButtonDialog(Activity activity, String title,
			String message, OnClickListener onOK, OnClickListener onNG) {
		show2ButtonDialog(activity, title, message, OK, CANCEL, onOK, onNG,
				null);
	}

	public final static void show2ButtonDialog(Activity activity, String title,
			String message, String labelOK, String labelNG,
			OnClickListener onOK, OnClickListener onNG) {
		show2ButtonDialog(activity, title, message, labelOK, labelNG, onOK,
				onNG, null);
	}

	public final static void show2ButtonDialog(Activity activity,
			String message, String labelOK, String labelNG,
			OnClickListener onOK, OnClickListener onNG) {
		show2ButtonDialog(activity, null, message, labelOK, labelNG, onOK,
				onNG, null);
	}

	public final static void show2ButtonDialog(Activity activity, String title,
			String message, String labelOK, String labelNG,
			OnClickListener onOK, OnClickListener onNG,
			OnDismissListener onDismiss) {
		if (activity.isFinishing())
			return;
		AlertDialog dialog = new AlertDialog.Builder(activity)
				.setMessage(message).setPositiveButton(labelOK, onOK)
				.setNegativeButton(labelNG, onNG).create();
		if (!TextUtils.isEmpty(title)) {
			// dialog.setTitle(title);
			dialog.setCustomTitle(createTitleView(activity, title));
		}
		if (onDismiss != null) {
			dialog.setOnDismissListener(onDismiss);
		}
		dialog.show();
	}

	public final static void show2ButtonDialog(Activity activity, String title,
			String message, String labelOK, String labelNG,
			OnClickListener onOK, OnKeyListener onKeyListener) {
		if (activity.isFinishing())
			return;
		AlertDialog dialog = new AlertDialog.Builder(activity)
				.setMessage(message).setPositiveButton(labelOK, onOK)
				.setNegativeButton(labelNG, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}).create();
		if (!TextUtils.isEmpty(title)) {
			// dialog.setTitle(title);
			dialog.setCustomTitle(createTitleView(activity, title));
		}
		if (onKeyListener != null) {
			dialog.setOnKeyListener(onKeyListener);
		}
		dialog.show();
	}

	public static TextView createTitleView(Activity activity, String title) {
		TextView tvTitle = new TextView(activity);
		tvTitle.setText(title);
		tvTitle.setPadding(10, 10, 10, 10);
		tvTitle.setGravity(Gravity.CENTER);
		tvTitle.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT));
		// title.setTextColor(getResources().getColor(R.color.greenBG));
		tvTitle.setTextSize(21);
		tvTitle.setBackgroundColor(Color.TRANSPARENT);
		return tvTitle;

	}

	/*************************** Loading dialog ***************************************/
	public static class Loading {
		private static ProgressDialog sDialog;
		private static boolean cancelFlag = false;

		public static void show(Activity context, boolean isNegativeBtn,
				final CancelListener cancelListener) {
			if (context == null)
				return;
			if (sDialog != null && sDialog.isShowing()) {
				sDialog.dismiss();
			}
			cancelFlag = false;
			sDialog = new ProgressDialog(context);
			sDialog.setMessage("Loading");
			sDialog.setCancelable(false);
			if (isNegativeBtn) {
				sDialog.setButton(ProgressDialog.BUTTON_NEGATIVE,
						context.getString(android.R.string.cancel),
						new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								cancelFlag = true;
								if (cancelListener != null) {
									cancelListener.onCancelListener(dialog,
											which);
								}
							}
						});
			}
			sDialog.show();
		}

		public static void dismiss() {
			try {
				if (sDialog != null && sDialog.isShowing()) {
					sDialog.dismiss();
				}
			} catch (IllegalArgumentException e) {
				Logger.error("Loading", "Could not dismiss loading dialog.");
			}
			sDialog = null;
		}

		public static boolean isCancel() {
			return cancelFlag;
		}
	}

	public static interface CancelListener {
		public void onCancelListener(DialogInterface dialog, int which);
	}

}
